
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/home/main_page.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

String _userErrorText;
String _passwordErrorText;
_UserFieldState _userFieldState;
_PasswordFieldState _passwordFieldState;

class Login extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    return LoginState();
  }
}

class LoginState extends State{
  final TextEditingController userTextEdit = new TextEditingController();
  final TextEditingController passwordTextEdit = new TextEditingController();
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: '账户登录',
        locale: Locale('zh'),//添加中文语言支持
        supportedLocales: [ const Locale.fromSubtags(languageCode: 'zh'), ],
        localizationsDelegates: [
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
        ],
        home: Scaffold(
          appBar: AppBar(
            leading: IconButton(
              icon: Icon(Icons.arrow_back),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            title: Text("登录"),
          ),
          body: Container(
            child: Stack(
              children: <Widget>[
                Positioned(
                  bottom: 36,
                  left: 12,
                  child: Container(
                    alignment: Alignment.centerLeft,
                    margin: EdgeInsets.fromLTRB(0, 6, 24, 0),
                    child: RaisedButton(
                      child: Text("注册账户"),
                      textColor: Colors.blue,
                      onPressed: (){
                        Navigator.pushNamed(context, "Register");
                      },
                      color: Colors.transparent,
                      disabledColor: Colors.blue,
                      elevation: 0,
                      highlightElevation: 0,
                    ),
                  ),
                ),
                Positioned(
                  bottom: 36,
                  right: 24,
                  child: Container(
                    child: RaisedButton(
                      child: Text("下一步"),
                      textColor: Colors.white,
                      onPressed: (){
                        if(userTextEdit.text.length != 0){
                          //更新userTextField取消警告
                          _userFieldState.setState((){
                            _userErrorText = null;
                          });
                          if(passwordTextEdit.text.length != 0) {
                            _passwordFieldState.setState(() {
                              _passwordErrorText = null;
                              //两次校验均合格
                              Navigator.pushAndRemoveUntil(context, MaterialPageRoute(
                                builder: (context) => MainPage(userTextEdit.text,passwordTextEdit.text),
                              ),(route) => route == null);
//                                      Navigator.pushNamedAndRemoveUntil(context, "MainPage", (route) => route == null);
                            });
                          } else if(passwordTextEdit.text.length == 0){
                            _passwordFieldState.setState(() {
                              _passwordErrorText = '密码不能为空';
                            });
                          }

                        } else if (userTextEdit.text.length == 0){
                          _userFieldState.setState(() {
                            //添加警告
                            _userErrorText = '用户名不能为空';
                          });
                        } else if(passwordTextEdit.text.length == 0){
                          _passwordFieldState.setState(() {
                            _passwordErrorText = '密码不能为空';
                          });
                        }
                      },
                      color: Colors.blue,
                      disabledColor: Colors.blue,
                    ),
                  ),
                ),
                Positioned(
                  child: Column(
                    children: <Widget>[
                      Padding(
                        padding: EdgeInsets.all(12.0),
                        child: Text(
                          '登录',
                          style: TextStyle(
                            color: Colors.black,
                            fontSize: 28.0,
                            height: 1.2,
                          ),
                        ),
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        mainAxisSize: MainAxisSize.max,
                        children: <Widget>[
                          Text(
                            '请登录您的账号。',
                            style: TextStyle(
                                color: Colors.black, fontSize: 15.0, height: 1.0),
                          ),
                          Text(
                            '了解详情',
                            style: TextStyle(
                                color: Colors.blue, fontSize: 15.0, height: 1.0),
                          )
                        ],
                      ),
                      Container(
                          margin: EdgeInsets.fromLTRB(24, 12, 24, 12),
                          child:_UserTextFiled("请输入您的用户名", "用户名",userTextEdit)
                      ),
                      Container(
                        margin: EdgeInsets.fromLTRB(24, 12, 24, 12),
                        child: _PasswordField("请输入密码", "密码",passwordTextEdit),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        )
    );
  }
}


class _UserTextFiled extends StatefulWidget{
  final String _hint;
  final String _labelText;
  final TextEditingController _textEditingController;
  _UserTextFiled(this._hint,this._labelText,this._textEditingController);
  @override
  State<StatefulWidget> createState() {
    return _userFieldState = _UserFieldState(_hint,_labelText,_textEditingController);
  }

}

class _UserFieldState extends State{
  final TextEditingController _textEditingController;
  final String _hintText;
  final String _labelText;
  _UserFieldState(this._hintText,this._labelText,this._textEditingController);
  @override
  void initState() {
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return TextField(
      keyboardType: TextInputType.text,
      decoration: InputDecoration(
        contentPadding: EdgeInsets.all(16),
        hintText: _hintText,
        errorText: _userErrorText,
        prefixIcon: Icon(Icons.person),
        enabledBorder: OutlineInputBorder(
          borderSide: BorderSide(
            width: 1,
          ),
          borderRadius: BorderRadius.all(const Radius.circular(5)),
        ),
        focusedBorder: OutlineInputBorder(
          borderSide: BorderSide(
            width: 3,
            color: Colors.blue,
          ),
          borderRadius: BorderRadius.all(const Radius.circular(5)),
        ),
        errorBorder: OutlineInputBorder(
          borderSide: BorderSide(
            width: 3,
            color: Colors.red,
          ),
          borderRadius: BorderRadius.all(const Radius.circular(5)),
        ),
        focusedErrorBorder: OutlineInputBorder(
          borderSide: BorderSide(
            width: 3,
            color: Colors.red,
          ),
          borderRadius: BorderRadius.all(const Radius.circular(5)),
        ),
        labelText: _labelText,
      ),
      controller: _textEditingController,
    );
  }

}

class _PasswordField extends StatefulWidget{
  final String _hintText;
  final String _labelText;
  final TextEditingController _textEditingController;
  _PasswordField(this._hintText,this._labelText,this._textEditingController);
  @override
  State<StatefulWidget> createState() {
    return _passwordFieldState = _PasswordFieldState(_hintText,_labelText,_textEditingController);
  }

}

class _PasswordFieldState extends State{
  bool canSee;
  final TextEditingController _textEditingController;
  final String _hintText;
  final String _labelText;
  _PasswordFieldState(this._hintText,this._labelText,this._textEditingController);

  @override
  void initState() {
    super.initState();
    canSee = false;
  }
  @override
  Widget build(BuildContext context) {
    return TextField(
      decoration: InputDecoration(
        contentPadding: EdgeInsets.all(16),
        hintText: _hintText,
        errorText: _passwordErrorText,
        prefixIcon: Icon(Icons.lock),
        suffixIcon: IconButton(
          icon: Icon(canSee ? Icons.visibility : Icons.visibility_off),
          onPressed: (){
            setState(() {
              canSee = !canSee;
            });
          },
        ),
        enabledBorder: OutlineInputBorder(
          borderSide: BorderSide(
            width: 1,
          ),
          borderRadius: BorderRadius.all(const Radius.circular(5)),
        ),
        focusedBorder: OutlineInputBorder(
          borderSide: BorderSide(
            width: 3,
            color: Colors.blue,
          ),
          borderRadius: BorderRadius.all(const Radius.circular(5)),
        ),
        errorBorder: OutlineInputBorder(
          borderSide: BorderSide(
            width: 3,
            color: Colors.red,
          ),
          borderRadius: BorderRadius.all(const Radius.circular(5)),
        ),
        focusedErrorBorder: OutlineInputBorder(
          borderSide: BorderSide(
            width: 3,
            color: Colors.red,
          ),
          borderRadius: BorderRadius.all(const Radius.circular(5)),
        ),
        labelText: _labelText,
      ),
      obscureText: !canSee,
      controller: _textEditingController,
    );
  }
}